<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<%@ include file="../common.jsp"%>
<%@ page import="cn.firstsoft.firstframe.admin.*" %>
<%@ page import="java.io.*,java.util.*" %>
<%@ page import="org.jdom.*,org.jdom.input.*,org.jdom.output.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>管理控制台 - 服务信息 - 修改端口</title>
<%@ include file="/common/meta2.jsp"%>
<link href="../style.css" type="text/css" rel=stylesheet>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%">
  <tr>
  	<td width="100%">
<%
	String port = request.getParameter("port");
	String old = String.valueOf(request.getServerPort());	
	if (port != null && !"".equals(port)) {
		if (!port.equals(old)) {
			String xmlFile = "";
			SAXBuilder builder = new SAXBuilder();
			Document doc;
			try {		
				if ("TOMCAT".equals(Environment.getWebServerName())) {
					//修改server.xml
					xmlFile = Environment.getWebServerPath() + File.separator + "conf" + File.separator + "server.xml";
					File file = new File(xmlFile);
					
					doc = builder.build(file);
					Element server = doc.getRootElement();
					Element service = server.getChild("Service");
					Element connector = service.getChild("Connector");
					//设置属性窗口

					connector.setAttribute("port", port);
					XMLOutputter outp = new XMLOutputter();
					outp.setFormat(Format.getPrettyFormat());
					outp.output(doc, new FileOutputStream(file));	
					out.println("修改文件："+xmlFile);
					out.println("<br>文件修改成功！需要重新启动服务器！");
				} else if ("APUSIC".equals(Environment.getWebServerName())) {
					//修改apusic.conf
					xmlFile = Environment.getWebServerPath() + File.separator + "config" + File.separator + "apusic.conf";
					File file = new File(xmlFile);
					doc = builder.build(file);
					Element config1 = doc.getRootElement();
					List serviceList = config1.getChildren("SERVICE");
					for (int i=0; i<serviceList.size(); i++) {
						Element service = (Element)serviceList.get(i);
						if ("com.apusic.net.Muxer".equals(service.getAttributeValue("CLASS"))
							|| "com.apusic.servlet.http.WebService".equals(service.getAttributeValue("CLASS"))) {
							List attributeList = service.getChildren("ATTRIBUTE");
							for (int j=0; j<attributeList.size(); j++) {
								Element attribute = (Element)attributeList.get(j);
								if ("Port".equals(attribute.getAttributeValue("NAME"))
								||"ServerPort".equals(attribute.getAttributeValue("NAME"))) {
									attribute.setAttribute("VALUE", port);
								}
							}
						}
					}
					XMLOutputter outp = new XMLOutputter();
					outp.setFormat(Format.getPrettyFormat());
					outp.output(doc, new FileOutputStream(file));
					out.println("修改文件："+xmlFile);
					out.println("<br>文件修改成功！需要重新启动服务器！");
				} else {
					out.println("不知道使用的是那种应用服务器，不能进行修改！");
				}
			} catch (JDOMException e) {
				out.println("修改失败："+e.getMessage());
				e.printStackTrace();
			} catch (IOException e) {
				out.println("修改失败："+e.getMessage());
				e.printStackTrace();
			} catch (Exception e) {
				out.println("修改失败："+e.getMessage());
				e.printStackTrace();
			}
		} else {
			out.println("您输入新的端口号与当前服务的端口号相同不能进行修改！");
		}
	} else {
		out.println("您输入新的端口号为空，请输入新的端口再进行修改端口！");
	}
%>
  	</td>
  </tr>
  <tr>
    <td>
    	<a href="javascript:void(0);" onclick="window.close();">关闭此窗口</a>
  	</td>
  </tr>  
</table>
</body>
</html>
